<template>
  <div id="category_id">
    <div class="menu_top">
      <p class="menu_note">
        注:
        <br />1.增加的新栏目为当前栏目下一行
        <br />2.修改完的栏目一行需保存后提交审批才能生效
      </p>
      <div class="menu_add">
        <el-button size="mini" round @click="addcategory">增加</el-button>
      </div>
    </div>
    <el-table
      :data="category_list"
      style="width: 100%;margin-top: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="编号" width="180"></el-table-column>
      <el-table-column prop="catename" label="类别名称" width="180"></el-table-column>
      <el-table-column prop="img" label="类别照片" v-slot="$props">
        <template>
          <img :src="'http://127.0.0.1:3000'+$props.row.img" alt />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" v-slot="$props">
        <template>
          <el-button size="mini" round type="success" v-if="$props.row.status==1">已启用</el-button>
          <el-button size="mini" round type="danger" v-else>已禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-slot="$props">
        <el-button size="mini" round type="warning" @click="setCategory($props.row)">修改</el-button>
        <el-button size="mini" round type="danger" @click="delCategory($props.row.id)">删除</el-button>
      </el-table-column>
    </el-table>
    <AddCategory
      ref="category_ref"
      @reloadCate="reloadCate"
      :isShow="isShow"
      :category_list="category_list"
    ></AddCategory>
  </div>
</template>

<script>
import AddCategory from "../views/AddCategory.vue";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      isShow: { filg: false }
    };
  },
  created() {
    this.CategoryAction();
  },
  methods: {
    delCategory(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$message({
          type: "success",
          message: "删除成功!"
        });

        this.$http.post("/catedelete", { id }).then(res => {
          if (res.code == 200) {
            this.CategoryAction();
          }
        });
      }).catch(()=>{
         this.$message({
            type: "info",
            message: "已取消删除"
          });
      })
    },
    setCategory(obj) {
      this.isShow.filg = true;
      this.$refs.category_ref.setcategory(obj);
    },
    reloadCate() {
      this.CategoryAction();
    },
    addcategory() {
      this.isShow.filg = true;
    },
    ...mapActions("CategoryStory", ["CategoryAction"])
  },
  computed: {
    ...mapState({ category_list: state => state.CategoryStory.category_list })
  },
  components: {
    AddCategory
  }
};
</script>

<style>
#category_id .menu_top {
  display: flex;
  justify-content: space-between;
}
#category_id .menu_note {
  font-size: 12px;
  color: #c4c4c4;
  text-align: left;
}
#category_id .menu_add > button {
  width: 80px;
  color: #ff5d3e;
  background-color: white;
  text-align: center;
}
</style>